package com.wg.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wg.R;
import com.wg.entity.*;
import com.wg.entity.dto.ApplyDto;
import com.wg.handler.WgException;
import com.wg.mapper.ApplyMapper;
import com.wg.service.ApplyService;
import com.wg.service.DeptService;
import com.wg.service.SubtaskService;
import com.wg.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class ApplyServiceImpl extends ServiceImpl<ApplyMapper, Applys> implements ApplyService {

    @Autowired
    UserService userService;

    @Autowired
    DeptService deptService;

    @Autowired
    SubtaskService subtaskService;

    @Override
    public List<Applys> getApplyByWg(Long id) {
        LambdaQueryWrapper<Applys> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Applys::getWgId, id);
        return list(wrapper);
    }

    @Override
    public R addApply(ApplyDto apply) {
        Applys dbApply = getApplyByUserWg(apply.getUserId(), apply.getWgId());
        if (dbApply != null) {
            throw new WgException(400, "您已申请加入此任务，请等待审批");
        }
        dbApply = new Applys();
        dbApply.setUserId(apply.getUserId()).setWgId(apply.getWgId()).setDescription(apply.getDescription());
        save(dbApply);
        return R.ok(200, "您已申请加入此任务，请等待审批");
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public R updateApply(ApplyDto apply) {
        Applys dbApply = getApplyByUserWg(apply.getUserId(), apply.getWgId());
        if (dbApply == null) {
            throw new WgException(400, "申请对象不存在");
        }
        dbApply.setStatus(apply.getStatus());
        updateById(dbApply);
        // 如果是设置成1，则新建一个子任务
        if(apply.getStatus() == 1) {
            Subtask task = new Subtask();
            task.setWgId(apply.getWgId());
            task.setAssignId(apply.getUserId());
            subtaskService.saveOrUpdate(task);
        }
        return R.ok();
    }

    @Override
    public R<List<Applys>> queryApplyByWg(Long wgId) {
        List<Applys> result = getApplyByWg(wgId);
        result = result.stream().map(
                apply -> {
                    Users user = userService.getById(apply.getUserId());
                    Depts dept = deptService.getById(user.getDeptId());
                    apply.setUserName(user.getName()).setEmail(user.getEmail());
                    if (dept != null) {
                        apply.setDept(dept.getName());
                    }
                    return apply;
                }
        ).collect(Collectors.toList());
        return R.ok(result);
    }

    public Applys getApplyByUserWg(Long userId, Long wgId) {
        LambdaQueryWrapper<Applys> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Applys::getUserId, userId).eq(Applys::getWgId, wgId);
        return getOne(wrapper);
    }
}
